package com.qf.threestagekaohe.handler;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.threestagekaohe.pojo.Dictionary;
import com.qf.threestagekaohe.service.DictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/dictionary")
public class DictionaryHandler {
    @Resource
    DictionaryService dictionaryService;
    @RequestMapping("/getDictionaries")
    @ResponseBody
    public PageInfo<Dictionary> users(@RequestParam(value = "pn",defaultValue = "1") Integer pn) throws IOException {
        PageHelper.startPage(pn,10);
        System.out.println("分页查询控制器");
        List<Dictionary> all = dictionaryService.getAll();
        System.out.println(dictionaryService == null);
        System.out.println(all);
        PageInfo<Dictionary> pageInfo = new PageInfo<>(all,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }

    @RequestMapping("/searchByFour")
    @ResponseBody
    public List<Dictionary> searchByFour(@RequestParam("name") String name, @RequestParam("type") String type, @RequestParam("status") String status, @RequestParam("stime") String stime, @RequestParam("etime") String etime){
        List<Dictionary> dictionaries = dictionaryService.searchByFour(name,type,status,stime,etime);
        return dictionaries;
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody Dictionary dictionary){
        int add = dictionaryService.add(dictionary);
        return add==1?"Y":"N";
    }


    @RequestMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody Dictionary dictionary){
        int edit = dictionaryService.edit(dictionary);
        return edit==1?"Y":"N";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id){
        int delete = dictionaryService.delete(id);
        return delete==1?"Y":"N";
    }

    @RequestMapping("/excelOut")
    @ResponseBody
    public List<Dictionary> excelOut() {
        List<Dictionary> all = dictionaryService.getAll();

        String fileName=".\\src\\main\\resources\\字典.xls";
        EasyExcel.write(fileName, Dictionary.class).sheet("easyexcel").doWrite(all);
        return all;
    }

    @RequestMapping("/modify")
    @ResponseBody
    public String modify(@RequestBody Dictionary dictionary){
        int modify = dictionaryService.modify(dictionary);
        return modify!=0?"Y":"N";
    }

    @RequestMapping("/alldel")
    @ResponseBody
    public String alldel(String sid){
        int alldel = dictionaryService.alldel(sid);
        return alldel!=0?"Y":"N";
    }


}
